/*
 * G53_UART_basic_example.c
 *
 * Created: 04/11/2014 05:48:15
 *  Author: Jerome Semette
 */ 

#include "sam.h"

void Console_init() 
{
	/*Enable Peripheral Clock */
	PMC->PMC_PCER0 |= (0x1 << ID_UART0) ;

	/* Configure UART IO */
	PIOA->PIO_PDR = (PIO_PDR_P10 | PIO_PDR_P9);

	/* Reset and disable receiver & transmitter */
	UART0->UART_CR = (UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS);
	UART0->UART_BRGR = (SystemCoreClock / 9600 ) / 16 ;
	/* Enable receiver and transmitter */
	UART0->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
		 
}

char UART_getc() {
	while(!(UART0->UART_SR & UART_SR_RXRDY));
	return  (char) UART0->UART_RHR;
}


void UART_putc(char Char) {
	while(!(UART0->UART_SR & UART_SR_TXRDY));
	UART0->UART_THR = (uint32_t) Char;
}


/**
 * \brief Application entry point.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
    /* Initialize the SAM system */
    SystemInit();
    uint8_t test_rx_char;
	SystemCoreClockUpdate();
	Console_init();
	
    while (1) 
    {
		// echo test
	    test_rx_char = UART_getc ();
		UART_putc(test_rx_char);
	}
}
